package core.tree;

import java.awt.Color;
import java.util.*;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Vertex{
    protected Vertex ptrToParent;
    protected LinkedList ptrToChildren, leafs;
    protected double distToParent;
    protected int xPos, yPos, i;

    private int diameter;
    private String title;
    private double childAngle = 0;
    private int rColIn, gColIn, bColIn;
    private int rColOut, gColOut, bColOut;
    private boolean highlight;


    public Vertex(int i){
        this.i = i;
        this.ptrToChildren = new LinkedList();
        this.distToParent = 0.00;
        this.leafs = new LinkedList();
        this.xPos = yPos = 0;
        title = null;
        diameter = 4;
        highlight = false;
        xPos = yPos = 0;
    }

    public Vertex(String t, int i){
        this.i = i;
        this.ptrToChildren = new LinkedList();
        this.distToParent = 0.00;
        this.leafs = new LinkedList();
        this.xPos = yPos = 0;
        title = t;
        diameter = 4;
        highlight = false;
        xPos = yPos = 0;
    }

    private void setLeafList(Vertex v){
        LinkedList children = v.getChildren();
        for(int i=0; i!=children.size(); i++){
           Vertex child = (Vertex)children.get(i);
           if(child.getNoOfChildren()==0){leafs.add(child);}
           else{setLeafList(child);}
        }
    }
    public int getNoLeaf(){leafs = new LinkedList(); setLeafList(this); return leafs.size();}
    public double getDistToParent(){return distToParent;}
    public void setDistToParent(double d){distToParent = d;}
    public void setPtrToParent(Vertex ptr){ptrToParent = ptr;}
    public Vertex ptrToParent(){return ptrToParent;}
    public void addChild(Vertex v){ptrToChildren.add(v); v.setPtrToParent(this);}
    public int getNoOfChildren(){return ptrToChildren.size();}
    public LinkedList getChildren(){return ptrToChildren;}
    public int getI(){return i;}
    public int getX(){return xPos;}
    public int getY(){return yPos;}
    public void setX(int x){xPos = x;}
    public void setY(int y){yPos = y;}
    public Vertex getPtrToParent(){return ptrToParent;}
    public LinkedList <Vertex> getLeafList(){
        leafs = new LinkedList();
        setLeafList(this);
        return leafs;
    }
    public int getDiameter(){return diameter;}
    public boolean getHighlight(){return highlight;}
    public void setHighlight(boolean b){highlight = b;}
    public void setCols(int r, int g, int b){setColIn(r, g, b);setColOut(r, g, b);}
    public void setColIn(int r, int g, int b){rColIn = r;gColIn = g;bColIn = b;}
    public Color getColIn(){return new Color(rColIn, gColIn, bColIn);}
    public void setColOut(int r, int g, int b){rColOut = r;gColOut = g;bColOut = b; }
    public Color getColOut(){return new Color(rColOut, gColOut, bColOut);}
    public void setOutterCol(Color c){
        this.setColOut(c.getRed(),c.getGreen(),c.getBlue());
        if(this.getNoOfChildren()>0){
            Object [] children = this.getChildren().toArray();
            for(int i=0; i!=children.length; i++){
               Vertex child = (Vertex)children[i];
               child.setColOut(c.getRed(),c.getGreen(),c.getBlue());
               if(child.getNoOfChildren()>0){child.setOutterCol(c);}
            }
        }
    }
    public boolean inTitle(String str){return (title.indexOf(str)==-1) ? false : true;}
    public String getTitle(){return title;}
    public double getChildAngle(){return childAngle;}
}